-- Universal is where I put functions that are usefull any time using the love engine.
-- Feel free to stick this lua in your own game and use it however you want.  I consider this a third party
-- library with no copyright.

function scaleToSize(pos, image)
  local width = pos[1]
  local height = pos[2]
  return {(width/image:getWidth()),(height/image:getHeight())}
end

function mouseIsTouching(xywh)
  local mx = love.mouse.getX()
  local my = love.mouse.getY()
  local x = xywh[1]
  local y = xywh[2]
  local w = xywh[3]
  local h = xywh[4]

  if (mx>x and mx<x+w) and (my>y and my<y+h) then
    return true
  else 
    return false
  end
end

function ScrW()
  return love.graphics.getWidth()
end

function ScrH()
  return love.graphics.getHeight()
end

function distance(x1,y1,x2,y2)
  return math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
end

function math.limit(num, min, max)
  if num < min then
    num = min
  elseif num > max then
    num = max
  end
  return num
end

function math.crot(color)
  -- rotate color
  return 255 - color % 255
end

function math.climit(color)
  return math.limit(color, 0, 255)
end

function printTable(tbl, indent)
  if not indent then indent = 3 end

  for k, v in pairs(tbl) do
    formatting = string.rep(" ", indent) .. k .. ": "
    if type(v) == "table" then
      print(formatting)
      printTable(v, indent+1)
    else
      print(formatting .. tostring(v))
    end
  end
end


function mouseIsTouchingLayer(xywhtable)
  local hit = false
  for i, v in pairs(xywhtable) do
    local mx = love.mouse.getX()
    local my = love.mouse.getY()
    local x = v[1]
    local y = v[2]
    local w = v[3]
    local h = v[4]

    if (mx>x and mx<x+w) and (my>y and my<y+h) then
      hit = true
    end
  end
  return hit
end

function tryCall(func)
  if func then 
    func() 
    return true
  else 
    --print("did not call") 
  end
end

-- Garry's mod has poisonned my mind to not remember which one I
-- had in this code, so I'm allowing both.  Going to start doing this
-- in my gmod code too...
function table.hasValue( t, v)
  for k,vl in pairs(t) do
    if vl == v then 
      return k
    end
  end
  return false
end
function table.HasValue( t, v)
  for k,vl in pairs(t) do
    if vl == v then 
      return k
    end
  end
  return false
end

function radianOverflow(radian)
  if radian < math.pi then return radian
    else return radian - math.pi*(radian/math.pi) end
end






















-- Hook System
-- You may want to just comment this out, it makes me feel at home having these guys around

hook = {}
hooksTable = {}

function hook.Call(funcCalled, args)
  for i, v in ipairs(hooksTable) do 
    if v[1] == funcCalled then
      local func = v[3]
      if not func then printTable(hooksTable) end
      func(args)
    end
  end
end

function hook.Remove(name)
  for i, v in ipairs(hooksTable) do
    if v[2]==name then
      table.remove(hooksTable, i)
    end
  end
end

function hook.Add(funcCalled, name, funcToRun)
  local found = false
  for i, v in ipairs(hooksTable) do 
    if v[2] == name then
      v = {funcCalled, name, funcToRun}
      found = true
    end
  end
  if not found then table.insert( hooksTable, {funcCalled, name, funcToRun} ) end
end

function hook.Exists(name)
  for i, v in ipairs(hooksTable) do 
    if v[2] == name then
      return true
    end
  end
  return false
end